<?php
include '../business/dao/BaseDataAccess.php';
include '../business/dao/ProductAccess.php';
include '../business/dao/ResourceAccess.php';
include '../business/model/model.php';
ini_set('display_errors',1);

$productDao = new ProductDao();

$action = getParameter("act");
$id = getParameter("id");
$catalog = getParameter("catalog");
$name = getParameter("name");
$content = getParameter("content");

if ("delete" == $action) {
	$productDao->deleteProduct($id);
	header("Location:product_list.php");
} else if ("save" == $action) {
	$product = new Product();
	$product->setId($id);
	$product->setCatalog($catalog);
	$product->setContent($content);
	$product->setName($name);
	$product->setCount(0);
	$resources = array();
	for ($i = 0; $i < 3; $i++) {
		$fileName = $_FILES["addImage$i"]['name'];
	
		if($fileName == null) {
			continue;
		}
	
		$fileSize = $_FILES["addImage$i"]['size'];
		$fileType = $_FILES["addImage$i"]['type'];
		$isThumbnail = isset($_POST["isThumbnail$i"]) ? true : false;
		$form_data = $_FILES["addImage$i"]['tmp_name'];
		$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
	
		$resource = new Resource();
		$resource->setData($data);
		$resource->setFileName($fileName);
		$resource->setFileSize($fileSize);
		$resource->setFileType($fileType);
		$resource->setType("img");
	
		if ($isThumbnail) {
			$resource->setIsThumbnail(TRUE_CHAR);
		} else {
			$resource->setIsThumbnail(FALSE_CHAR);
		}
		$resources[i] = $resource;
	}
	
	if ($id != null) {
		
	} else {
		$productDao->addProduct($product, $resources);
		header("Location:product_list.php");
	}

}

function getParameter($name) {
	
	if (isset($_GET[$name])) {
		return $_GET[$name];
	}
	
	if (isset($_POST[$name])) {
		return $_POST[$name];
	}
	
	return null;
	
}
?>